package com.kgc.controller;

import com.kgc.common.Result;
import com.kgc.entity.Comments;
import com.kgc.entity.PointsLog;
import com.kgc.entity.PointsRules;
import com.kgc.service.interf.CommentsService;
import com.kgc.service.interf.PointsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/apis/user/comment")
public class CommentsController {
    @Autowired
    private CommentsService commentsService;
    @Autowired
    private PointsService pointsService;

    /**
     * 查询所有评论
     *
     * @param comments
     * @return
     */
    @RequestMapping("/show")
    private String show(Comments comments) {
        System.out.println(comments);
        List<Comments> listComments = commentsService.selectAll(comments);
        if (comments != null) {
            return Result.success("200", "查询评论成功", listComments);
        } else {
            return Result.success("400", "不合法参数");
        }
    }

    /**
     * 添加评论
     *
     * @param comments
     * @return
     */
    @RequestMapping("/add")
    private String add(Comments comments) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        comments.setCreatedAt(simpleDateFormat.format(new Date()));
        int i = commentsService.addComments(comments);

        //获取发布文章对应的积分
        PointsRules pointsRules = new PointsRules();
        pointsRules.setRuleName("发表评论");
        List<PointsRules> points = pointsService.getPoints(pointsRules);
        int i1 = 0;
        int i2 = 0;
        for (PointsRules rules : points) {
            // 获取每个对象中的pointsValue属性值
            int pointsValue = rules.getPointsValue();
            int ruleID = rules.getRuleID();
            // 给用户增加积分
            i1 = pointsService.addPoints(comments.getUserID(), pointsValue);
            //添加积分流水
            PointsLog pointsLog = new PointsLog();
            pointsLog.setUserID(comments.getUserID());
            pointsLog.setRuleID(ruleID);
            pointsLog.setDescription("发表了一段评论");
            pointsLog.setCreatedAt(simpleDateFormat.format(new Date()));
            i2 = pointsService.addPointsFlow(pointsLog);
        }

        if (i > 0 && i1 > 0 && i2 > 0) {
            return Result.success("200", "添加评论成功");
        } else {
            return Result.success("400", "添加评论失败");
        }
    }

    /**
     * 删除个人评论
     *
     * @param comments
     * @return
     */
    @RequestMapping("/delByUserid")
    private String delByUserid(Comments comments) {
        if (comments.getUserID() == null && comments.getCommentId() == null){
            return Result.success("400", "参数不合法");
        }
        int i = commentsService.delByUserid(comments);
        if (i > 0) {
            return Result.success("200", "删除评论成功");
        } else {
            return Result.success("400", "抱歉,你没有权限");
        }
    }

    /**
     * 管理员删除评论
     *
     * @param comments
     * @return
     */
    @RequestMapping("/delComments")
    private String delComments(Comments comments) {
        int i = commentsService.delComments(comments);
        if (i > 0) {
            return Result.success("200", "删除评论成功");
        } else {
            return Result.success("400", "删除失败");
        }
    }

}
